package com.example.dsd.heimachengxuyuan.File_Access_Permission;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dsd.heimachengxuyuan.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FileAccessPermission extends AppCompatActivity {

    @Bind(R.id.btnCreatePrivateFile)
    Button btnCreatePrivateFile;
    @Bind(R.id.btnCreateReadOnlyFile)
    Button btnCreateReadOnlyFile;
    @Bind(R.id.btnCreateWriteOnlyFile)
    Button btnCreateWriteOnlyFile;
    @Bind(R.id.btnCreatePublicFile)
    Button btnCreatePublicFile;
    @Bind(R.id.txtShowFileContent_filePermission)
    TextView txtShowFileContentFilePermission;
    @Bind(R.id.btnShowPrivateFileContent)
    Button btnShowPrivateFileContent;
    @Bind(R.id.btnShowReadOnlyFileContent)
    Button btnShowReadOnlyFileContent;
    @Bind(R.id.btnShowWriteOnlyFilecontent)
    Button btnShowWriteOnlyFilecontent;
    @Bind(R.id.btnShowPublicFileContent)
    Button btnShowPublicFileContent;
    @Bind(R.id.btnCreatePrivateFile_sp)
    Button btnCreatePrivateFileSp;
    @Bind(R.id.btnCreateReadOnlyFile_sp)
    Button btnCreateReadOnlyFileSp;
    @Bind(R.id.btnCreateWriteOnlyFile_sp)
    Button btnCreateWriteOnlyFileSp;
    @Bind(R.id.btnCreatePublicFile_sp)
    Button btnCreatePublicFileSp;
    @Bind(R.id.btnShowPrivateFileContent_sp)
    Button btnShowPrivateFileContentSp;
    @Bind(R.id.btnShowReadOnlyFileContent_sp)
    Button btnShowReadOnlyFileContentSp;
    @Bind(R.id.btnShowWriteOnlyFileContent_sp)
    Button btnShowWriteOnlyFileContentSp;
    @Bind(R.id.btnShowPublicFileContent_sp)
    Button btnShowPublicFileContentSp;

    private SharedPreferences sp = null;
    private String content = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_access_permission);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btnCreatePrivateFile, R.id.btnCreateReadOnlyFile,
            R.id.btnCreateWriteOnlyFile, R.id.btnCreatePublicFile,
            R.id.btnShowPrivateFileContent, R.id.btnShowReadOnlyFileContent,
            R.id.btnShowWriteOnlyFilecontent, R.id.btnShowPublicFileContent,
            R.id.btnCreatePrivateFile_sp, R.id.btnCreateReadOnlyFile_sp,
            R.id.btnCreateWriteOnlyFile_sp, R.id.btnCreatePublicFile_sp,
            R.id.btnShowPrivateFileContent_sp, R.id.btnShowReadOnlyFileContent_sp,
            R.id.btnShowWriteOnlyFileContent_sp, R.id.btnShowPublicFileContent_sp})
    public void onClick(View view) {
        switch (view.getId()) {
/**********谷歌已经不推荐我们这样用了，引入了一个新的组件   contetnProvider**********/
            case R.id.btnCreatePrivateFile:
                /**
                 * 创建一个私有的文件
                 *    每个应用程序 都有一个私有文件夹 --->
                 *    date/date/con.example.dsd.heimachengxuyuan
                 * 私有文件只有自己能访问，其他应用 不可以读 也不可以写
                 */
                //try catch 快捷键 ：crl + alt + t
                File file = new File("/data/data/com.example.dsd.heimachengxuyuan/private.txt");
                try {
                    OutputStream outputStream = new FileOutputStream(file);
                    outputStream.write("private".getBytes());

                    btnShowPrivateFileContent.setEnabled(true);
                    btnShowPrivateFileContent.setFocusable(true);
                    btnShowPrivateFileContent.setClickable(true);

                    Toast.makeText(FileAccessPermission.this, "创建私有文件成功",
                            Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(FileAccessPermission.this, "创建私有文件成功 失败",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.btnCreateReadOnlyFile:
                /**
                 *创建只读文件，其他应用只可以去读，不能写
                 */
                try {
                    FileOutputStream out = openFileOutput("readOnly.txt",
                            Context.MODE_WORLD_READABLE);
                    out.write("readOnly".getBytes());
                    out.close();

                    btnShowReadOnlyFileContent.setEnabled(true);
                    btnShowReadOnlyFileContent.setFocusable(true);
                    btnShowReadOnlyFileContent.setClickable(true);

                    Toast.makeText(FileAccessPermission.this, "创建只读文件成功", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(FileAccessPermission.this, "创建只读文件失败", Toast.LENGTH_SHORT).show();

                }
                break;

            case R.id.btnCreateWriteOnlyFile:
                /**
                 *创建只可以写文件
                 */
                try {
                    FileOutputStream out = openFileOutput("writeOnly.txt",
                            Context.MODE_WORLD_WRITEABLE);
                    out.write("writeOnly".getBytes());
                    out.close();

                    btnShowWriteOnlyFilecontent.setClickable(true);
                    btnShowWriteOnlyFilecontent.setEnabled(true);
                    btnShowWriteOnlyFilecontent.setFocusable(true);

                    Toast.makeText(FileAccessPermission.this, "创建只写文件成功", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(FileAccessPermission.this, "创建只写文件失败", Toast.LENGTH_SHORT).show();

                }
                break;

            case R.id.btnCreatePublicFile:
                /**
                 *外部应用 可以读，也可以写的文件
                 */
                try {
                    FileOutputStream out = openFileOutput("public.txt",
                            Context.MODE_WORLD_READABLE + Context.MODE_WORLD_READABLE);
                    out.write("public".getBytes());
                    out.close();

                    btnShowPublicFileContent.setClickable(true);
                    btnShowPublicFileContent.setFocusable(true);
                    btnShowPublicFileContent.setEnabled(true);

                    Toast.makeText(FileAccessPermission.this, "创建公共(可读可写)文件成功",
                            Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(FileAccessPermission.this, "创建公共(可读可写)文件失败",
                            Toast.LENGTH_SHORT).show();
                }
                break;

/********openOutPut 访问文件内容************/

            case R.id.btnShowPrivateFileContent:
                File mFile = new File("/data/data/com.example.dsd.heimachengxuyuan/private.txt");
                try {
                    BufferedReader br = new BufferedReader(new FileReader(mFile));
                    String line = br.readLine();
                    txtShowFileContentFilePermission.setText(line);


                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case R.id.btnShowReadOnlyFileContent:
                File mFile1 = new File("/data/data/com.example.dsd" +
                        ".heimachengxuyuan/files/readOnly.txt");

                try {
                    BufferedReader br = new BufferedReader(new FileReader(mFile1));
                    String line = br.readLine();
                    txtShowFileContentFilePermission.setText(line);


                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case R.id.btnShowWriteOnlyFilecontent:
                File mFile2 = new File("/data/data/com.example.dsd." +
                        "heimachengxuyuan/files/writeOnly.txt");

                try {
                    BufferedReader br = new BufferedReader(new FileReader(mFile2));
                    String line = br.readLine();
                    txtShowFileContentFilePermission.setText(line);


                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case R.id.btnShowPublicFileContent:
                File mFile3 = new File("/data/data/com.example." +
                        "dsd.heimachengxuyuan/files/public.txt");

                try {
                    BufferedReader br = new BufferedReader(new FileReader(mFile3));
                    String line = br.readLine();
                    txtShowFileContentFilePermission.setText(line);


                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

/********SharedPreference创建文件内容************/

            /**
             * SharedPreference
             * getSharedPreferences("config",Context.MODE_PRIVATE)
             * config 文件将会生成在 应用的文件夹下 --->是一个XML文件
             * 一般情况下，应用自己的数据 只有 自己可以去读，所以通常会写 Context.MODE_PRIVATE == 0
             */
            case R.id.btnCreatePrivateFile_sp:

                try {
                    sp = getSharedPreferences("config", Context.MODE_PRIVATE);
                    //拿到sp的编辑器，使用键值对的方式写入数据
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("private", "private file by SharedPreferences");
                    //将数据提交，到private数据中
                    editor.commit();

                    Toast.makeText(FileAccessPermission.this, "创建私有文件成功",
                            Toast.LENGTH_SHORT).show();
                    btnShowPrivateFileContentSp.setEnabled(true);
                    btnShowPrivateFileContentSp.setFocusable(true);
                    btnShowPrivateFileContentSp.setClickable(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(FileAccessPermission.this, "创建私有文件成功 失败",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.btnCreateReadOnlyFile_sp:
                try {
                    sp = getSharedPreferences("config", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("readOnly", "readOnly file by SharedPreferences");
                    editor.commit();

                    Toast.makeText(FileAccessPermission.this, "创建只读文件成功",
                            Toast.LENGTH_SHORT).show();
                    btnShowReadOnlyFileContentSp.setEnabled(true);
                    btnShowReadOnlyFileContentSp.setFocusable(true);
                    btnShowReadOnlyFileContentSp.setClickable(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(FileAccessPermission.this, "创建只读文件成功 失败",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.btnCreateWriteOnlyFile_sp:
                try {
                    sp = getSharedPreferences("config", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("writeOnly", "writeOnly  file by SharedPreferences");
                    editor.commit();

                    Toast.makeText(FileAccessPermission.this, "创建只写文件成功",
                            Toast.LENGTH_SHORT).show();
                    btnShowWriteOnlyFileContentSp.setEnabled(true);
                    btnShowWriteOnlyFileContentSp.setFocusable(true);
                    btnShowWriteOnlyFileContentSp.setClickable(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(FileAccessPermission.this, "创建只写文件成功 失败",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.btnCreatePublicFile_sp:
                try {
                    sp = getSharedPreferences("config", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("public", "public  file by SharedPreferences");
                    editor.commit();

                    Toast.makeText(FileAccessPermission.this, "创建公共文件成功",
                            Toast.LENGTH_SHORT).show();
                    btnShowPublicFileContentSp.setEnabled(true);
                    btnShowPublicFileContentSp.setFocusable(true);
                    btnShowPublicFileContentSp.setClickable(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(FileAccessPermission.this, "创建公共文件成功 失败",
                            Toast.LENGTH_SHORT).show();
                }
                break;

/********SharedPreference 读取文件内容************/
            case R.id.btnShowPrivateFileContent_sp:
                //系统会自动检查，是否存在config文件，有过没有就创建。
                sp = getSharedPreferences("config", 0);

                //如果找到值 就返回，如果没有找到，就返回默认值

                content = sp.getString("private", "");
                txtShowFileContentFilePermission.setText(content);
                break;

            case R.id.btnShowReadOnlyFileContent_sp:
                sp = getSharedPreferences("config", 0);

                content = sp.getString("readOnly", "");
                txtShowFileContentFilePermission.setText(content);
                break;

            case R.id.btnShowWriteOnlyFileContent_sp:
                sp = getSharedPreferences("config", 0);
                content = sp.getString("writeOnly", "");
                txtShowFileContentFilePermission.setText(content);
                break;

            case R.id.btnShowPublicFileContent_sp:
                sp = getSharedPreferences("config", 0);
                content = sp.getString("public", "");
                txtShowFileContentFilePermission.setText(content);
                break;
        }
    }


}
